package com.sunb2b.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

public class WebUtils {
	public static boolean checkValidCode(HttpServletRequest request){
		String validCode = request.getParameter("validCode");
		String validCodeInSession = (String)request.getSession().getAttribute("validCode");
		if(validCode==null||validCodeInSession==null||!validCode.equals(validCodeInSession)){
			return false;
		}
		return true;
		
	}
	
	public static String getExtName(MultipartFile f){
		return f.getOriginalFilename().substring(f.getOriginalFilename().lastIndexOf("."));
	}
	
	public static boolean isPicture(MultipartFile f){
		Pattern p = Pattern.compile("^\\.(jpg|gif|png)$", Pattern.CASE_INSENSITIVE);
		Matcher m = p.matcher(getExtName(f));
		if(m.find()){
			return true;
		}
		return false;
	}
	
	public static boolean isAdmin(HttpServletRequest request){
		return (request.getSession().getAttribute("user")!=null);
	}
	
	public static boolean isMember(HttpServletRequest request){
		return (request.getSession().getAttribute("member")!=null);
	}
	
}
